#pragma once

#include "nlohmann/json.hpp"
#include <cstddef>
#include <string>

namespace Procotol {
class DeviceStatReportMsgObject {
    std::size_t tick;
    std::size_t time;
    std::string src;
    struct Dev {
        std::size_t id;
        std::string status;
        std::string sn;
        std::string hver;
        std::string sver;

        Dev(std::size_t id, bool isOnline)
            : id(id), status(isOnline ? "online" : "offline") {}
        Dev(std::size_t id, bool isOnline, const std::string &sn, const std::string &sver, const std::string &hver)
            : id(id), status(isOnline ? "online" : "offline"), sn(sn), sver(sver), hver(hver) {}

        Dev &setStatus(bool stat) {
            status = stat ? "online" : "offline";
            return *this;
        }

        friend void to_json(nlohmann::json &j, const Dev &d) {
            j = {{"id", d.id}, {"status", d.status}};
            if (!d.sn.empty()) {
                j["sn"] = d.sn;
            }

            if (!d.hver.empty()) {
                j["hver"] = d.hver;
            }

            if (!d.sver.empty()) {
                j["sver"] = d.sver;
            }
        }
    };

    std::vector<Dev> dev;

    friend void to_json(nlohmann::json &j, const DeviceStatReportMsgObject &d) {
        j = {{"tick", d.tick}, {"time", d.time}, {"src", d.src}, {"dev", d.dev}};
    }

public:
    DeviceStatReportMsgObject(std::size_t id, bool isOnline)
        : tick(1),
          time(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()),
          src("dev_dlt698"),
          dev({Dev(id, isOnline)}) {}
};
};  // namespace Procotol
